/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.views;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.business.Category;
import fightplan.business.TournamentHolder;
import fightplan.business.TournamentListener;
import fightplan.views.tables.CategoryTable;

@SuppressWarnings("serial")
public class CategoriesPanel extends JPanel {
	private CategoryTable table;

	public CategoriesPanel(final TournamentHolder tournamentHolder) {
		setLayout(new BorderLayout());

		table = new CategoryTable(tournamentHolder);
		final DrawingPanel drawingPanel = new DrawingPanel(tournamentHolder);

		FormLayout layout = new FormLayout("100px:grow ", "20dlu, 100px:grow:fill");
		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
		builder.setBorder(BorderFactory.createEmptyBorder(3, 12, 3, 12));

		builder.appendSeparator("Kategorien");

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setBorder(null);
		splitPane.setLeftComponent(table);
		splitPane.setRightComponent(drawingPanel);

		builder.append(splitPane);

		tournamentHolder.addValueChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				Category category = (Category) table.getSelectedCategory().getValue();
				drawingPanel.setCategory(category);
			}
		});

		table.getSelectedCategory().addValueChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				Category category = (Category) table.getSelectedCategory().getValue();
				drawingPanel.setCategory(category);
			}
		});

		tournamentHolder.addListener(new TournamentListener() {
			public void tournamentChanged() {
				Category category = (Category) table.getSelectedCategory().getValue();
				drawingPanel.setCategory(category);
			}
		});

		add(builder.getPanel(), BorderLayout.CENTER);
	}

	public List<Category> getSelectedCategories() {
		return table.getSelectedCategories();
	}

}
